package main

import (
	"boke/blogpb"
	"context"
	"fmt"
	"github.com/rs/zerolog/log"
)

// 处理新建blog请求
func (s *server) CreateBlog(ctx context.Context, req *blogpb.CreateBlogRequest) (*blogpb.CreateBlogResponse, error) {
	fmt.Println("Create blog request")

	blog := req.GetBlog()

	data := blogItem{
		AuthorID: blog.GetAuthorId(),
		Title:    blog.GetTitle(),
		Content:  blog.GetContent(),
	}
	fmt.Printf("req data:%+v\n", data)
	res, err := db.NamedExec(insert_sql, data)
	if err != nil {
		log.Error().Err(err).Msg("插入数据失败")

		return nil, err
	}

	bid, err := res.LastInsertId()
	if err != nil {
		return nil, err
	}
	fmt.Println("bid is %v", bid)
	return &blogpb.CreateBlogResponse{
		Blog: &blogpb.Blog{
			Id:       bid,
			AuthorId: blog.GetAuthorId(),
			Title:    blog.GetTitle(),
			Content:  blog.GetContent(),
		},
	}, nil
}
